//  微博首页cell
//  StatusCell.swift
//  WeiboSwift
//
//  Created by Apple13 on 16/7/14.
//  Copyright © 2016年 yupeng. All rights reserved.
//

import UIKit
import SnapKit

//设置控件的间距
let HomeCellMargin: CGFloat = 10


class StatusCell: UITableViewCell {
    
    //记录toob的顶部约束
   var toolbarTopConstraint: Constraint?
    
    
//MARK: - 懒加载控件
    
    
    //原创微博视图
    private lazy var originView: StatusOriginalView = StatusOriginalView()
    
    //转发微博视图
    private lazy var retweetView: StatusRetweetView = StatusRetweetView()
    
    //  toolbar
    private lazy var toolBar: WBStatusToolBar = WBStatusToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpUI()
    }
    
   
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    
//使用ViewModel模型
    var statusViewModel:StatusViewModel?{
        didSet{
            
            //给原创微博视图设置需要的视图模型
            originView.statusViewModel = statusViewModel
            
            //卸载约束
            toolbarTopConstraint?.uninstall()
            
            
            //判断是否有转发对象
            if statusViewModel?.status?.retweeted_status != nil {
                //  有转发微博数据
                //  给转发微博视图设置需要的视图模型, 显示转发微博视图,更新约束
                retweetView.statusViewModel = statusViewModel
                //
                retweetView.hidden = false
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    //  记录更新的约束
                    self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })


            }else {
                //  没有转发微博数据, 隐藏转发微博视图,更新约束
                retweetView.hidden = true
                
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    //  记录更新的约束
                    self.toolbarTopConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
                
            }
            
            //  给toolbar视图设置需要的视图模型
            toolBar.statusViewModel = statusViewModel

        }
        
        
        
    }
    
//MARK: - 设置UI添加视图约束
    private func setUpUI() {
        
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1) //增加cell间距①
        
        contentView.addSubview(originView)
         contentView.addSubview(retweetView)
         contentView.addSubview(toolBar)
        
        originView.snp_makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)  //增加cell间距②
//            make.top.equalTo(contentView).offset
            
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originView.snp_bottom)
            make.leading.equalTo(originView)
            make.trailing.equalTo(originView)
        }

        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }

        
//‼️ 重要约束
        contentView.snp_makeConstraints { (make) in
            make.bottom.equalTo(toolBar)
            //‼️临时设置
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }

}
